<template>
  <div class="register-container">
    <div class="register-box">
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="username" 
            required
            placeholder="请输入用户名"
          >
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="password" 
            required
            placeholder="请输入密码"
          >
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="confirmPassword" 
            required
            placeholder="请再次输入密码"
          >
        </div>
        <div class="error-message" v-if="errorMessage">
          {{ errorMessage }}
        </div>
        <div class="success-message" v-if="successMessage">
          {{ successMessage }}
        </div>
        <button type="submit" :disabled="isLoading">
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
      </form>
      <p class="login-link">
        已有账号？ <router-link to="/">立即登录</router-link>
      </p>
    </div>
  </div>
</template>

<script>
import { API_ENDPOINTS } from '../api/config';
import axios from 'axios';

export default {
  name: 'UserRegister',
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: '',
      errorMessage: '',
      successMessage: '',
      isLoading: false
    };
  },
  methods: {
    async handleRegister() {
      this.isLoading = true;
      this.errorMessage = '';
      this.successMessage = '';

      if (this.password !== this.confirmPassword) {
        this.errorMessage = '两次输入的密码不一致';
        this.isLoading = false;
        return;
      }

      try {
        console.log('发送注册请求:', { username: this.username });

        const response = await axios.post(API_ENDPOINTS.AUTH.REGISTER, {
          username: this.username,
          password: this.password,
          name: this.username // 默认name与username相同，可以根据需求修改
          // 注册时role固定为'user', 后端会处理
        });

        console.log('注册响应:', response.data);

        if (response.data.message) {
          this.successMessage = response.data.message + '，即将跳转到登录页...';
          setTimeout(() => {
            this.$router.push('/');
          }, 2000); // 2秒后跳转
        }
      } catch (error) {
        console.error('注册失败:', error);
        if (error.response) {
          console.error('错误响应:', error.response.data);
          this.errorMessage = error.response.data.message || '注册失败，请稍后重试';
        } else if (error.request) {
          console.error('请求错误:', error.request);
          this.errorMessage = '网络错误，请检查网络连接';
        } else {
          console.error('其他错误:', error.message);
          this.errorMessage = '发生错误，请稍后重试';
        }
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f8f9fa;
}

.register-box {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 5px;
  color: #2c3e50;
}

input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

input:focus {
  outline: none;
  border-color: #42b983;
}

button {
  width: 100%;
  padding: 12px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background: #3aa876;
}

button:disabled {
  background: #a8d5c2;
  cursor: not-allowed;
}

.error-message {
  color: #dc3545;
  margin-bottom: 15px;
  text-align: center;
}

.success-message {
  color: #28a745;
  margin-bottom: 15px;
  text-align: center;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  font-size: 0.9em;
}

.login-link a {
  color: #42b983;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}
</style> 